import { CacheEnum } from '@renderer/enum/CacheEnum'
import router from '@renderer/plugins/router'
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
import { IMenu } from './menuAdmin'
import useStorage from './useStorage'
const storage = useStorage()
class Menu {
  public menus = ref<IMenu[]>([])
  public menuTitle = ref<string>('')
  public history = ref<IMenu[]>([])

  constructor() {
    ;(async () => {
      this.menus.value = await this.getMenuByRoute()
    })()
  }
  toggleParentMenu(menu: IMenu) {
    if (menu.isClick) {
      menu.isClick = false
      return
    }
    this.menus.value.forEach((m) => {
      if (m == menu) {
        m.isClick = true
      } else {
        m.isClick = false
      }
    })
  }
  // 设置当前菜单
  setCurrentMenu(routeName: string) {
    this.menus.value.forEach((m) => {
      m.isClick = false
      m.children?.forEach(async (c) => {
        c.isClick = false
        if (c.route == routeName) {
          m.isClick = true
          c.isClick = true
          const isStatus = await this.isSkipPage(c.title!)
          if (isStatus) {
            router.push({ name: c.route })
          } else {
            m.isClick = false
            c.isClick = false
            ElMessage({ type: 'error', message: `抱歉！您没有此权限` })
          }
        }
      })
    })
  }
  async getMenuByRoute() {
    const routes = await Promise.all(
      router
        .getRoutes()
        .filter((route) => route.children.length && route.meta.menu?.isType == 'admin')
        .sort((a, b) => (b.meta.menu?.order ?? 0) - (a.meta.menu?.order ?? 0))
        .map(async (route) => {
          let menu: IMenu = { ...route.meta?.menu }
          if (menu.title == '项目文件' || menu.title == '共享文件') {
            const isStatus = await this.isSkipPage(menu.title)
            if (!isStatus) {
              return
            }
          }
          if (menu.title == '人员管理') {
            const isStatus = await this.isSkipPage(menu.title)
            if (!isStatus) {
              return
            }
          }
          menu.children = route.children
            .filter((route) => route.meta?.menu)
            .map((route) => {
              return { ...route.meta?.menu, route: route.name }
            }) as IMenu[]
          return menu
        }),
    )
    return routes.filter((menu) => menu && menu.children?.length) as IMenu[]
  }
  async isSkipPage(pageTitle: string) {
    const userInfo = await storage.get(CacheEnum.USER_INFO)
    const companyInfo = await storage.get(CacheEnum.COMPANY_INFO)
    switch (pageTitle) {
      case '项目文件':
        return (
          companyInfo.check_work == 2 &&
          (userInfo?.rule.includes('项目文件查看权') || userInfo?.super_check == 1) &&
          companyInfo.check_disk == 2
        )
      case '共享文件':
        if (
          (userInfo?.rule.includes('共享文档查看权') || userInfo?.super_check == 1) &&
          companyInfo.check_disk == 2 &&
          companyInfo.check_doc == 2
        ) {
          return true
        } else {
          return false
        }
      case '系统核算方式':
        return userInfo?.rule.includes('核算方式修改权') || userInfo?.super_check == 1
      case '人员管理':
        return (
          userInfo?.rule.includes('员工管理查看权') ||
          userInfo?.rule.includes('支出对象管理查看权') ||
          userInfo?.super_check == 1
        )
      case '损耗系数':
        return userInfo?.rule.includes('损耗系数修改权') || userInfo?.super_check == 1
      case '材料规格':
        return userInfo?.rule.includes('材料规格修改权') || userInfo?.super_check == 1
      case '数据导出':
        return (
          ((userInfo?.rule.includes('流水数据导出权') || userInfo?.super_check == 1) &&
            companyInfo.check_record == 2) ||
          userInfo?.rule.includes('库存数据导出权') ||
          userInfo?.super_check == 1
        )
      default:
        return true
    }
  }
}

export default new Menu()
